﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//
//select * from ead.TBL_AUTH_FAIL ORDER BY OCCURE_TIME DESC  --绑定检查失败
//select * from ead.TBL_AUTH_FAIL_REPORT ORDER BY OCCURE_TIME DESC --认证失败
using EntitiesModel;

namespace ITMan
{
    public partial class FormQueryOnlineUser : Form
    {
        public FormQueryOnlineUser()
        {
            InitializeComponent();
        }

        public BindingSource bindingSource = new BindingSource();

        private void btQuery1_Click(object sender, EventArgs e)
        {
            switch (comboCondition.SelectedIndex)
            {
                case -1:
                    {
                        dataGridView1.DataSource = null;
                        using (var eaddbContext = new EadEntitiesModel())
                        {
                            var OnlineUsers = from u in eaddbContext.Vw_onlines
                                              orderby u.FRAMED_IP
                                              select new { u.USER_NAME, u.FULL_NAME, u.FRAMED_IP, u.MAC_ADDRESS };
                            if (OnlineUsers.Any())
                            {
                                bindingSource.DataSource = OnlineUsers;
                                dataGridView1.DataSource = bindingSource;

                                dataGridView1.Columns["USER_NAME"].HeaderText = "登录名";
                                dataGridView1.Columns["FULL_NAME"].HeaderText = "姓名";
                                dataGridView1.Columns["FRAMED_IP"].HeaderText = "IP";
                                dataGridView1.Columns["MAC_ADDRESS"].HeaderText = "MAC地址";
                                toolStripStatusLabelUser.Text = (string.Format("已查询到在线用户{0}人", OnlineUsers.Count()));
                                this.Refresh();
                            }
                        }
                        break;
                    }
                case 0: //用户
                    {
                        if (!string.IsNullOrEmpty(tbContent.Text))
                        {
                            dataGridView1.DataSource = null;
                            using (var eaddbContext = new EadEntitiesModel())
                            {
                                var OnlineUsers = from u in eaddbContext.Vw_onlines
                                                  where u.USER_NAME.Contains(tbContent.Text) || u.FULL_NAME.Contains(tbContent.Text)
                                                  orderby u.FRAMED_IP
                                                  select new { u.USER_NAME, u.FULL_NAME, u.FRAMED_IP, u.MAC_ADDRESS };
                                if (OnlineUsers.Any())
                                {
                                    bindingSource.DataSource = OnlineUsers;
                                    dataGridView1.DataSource = bindingSource;

                                    dataGridView1.Columns["USER_NAME"].HeaderText = "登录名";
                                    dataGridView1.Columns["FULL_NAME"].HeaderText = "姓名";
                                    dataGridView1.Columns["FRAMED_IP"].HeaderText = "IP";
                                    dataGridView1.Columns["MAC_ADDRESS"].HeaderText = "MAC地址";
                                    toolStripStatusLabelUser.Text = "";
                                    this.Refresh();
                                }
                                else
                                {
                                    dataGridView1.DataSource = null;
                                    MessageBox.Show(string.Format("未查询到用户名或登录名为\"{0}\"'的在线用户!", tbContent.Text));
                                }
                            }
                        }
                        else
                        {
                            goto case -1;
                        }

                        break;
                    }
                case 1: //IP
                {
                    if (!string.IsNullOrEmpty(tbContent.Text))
                    {
                        dataGridView1.DataSource = null;
                        using (var eaddbContext = new EadEntitiesModel())
                        {
                            var OnlineUsers = from u in eaddbContext.Vw_onlines
                                              where u.FRAMED_IP.Contains(tbContent.Text)
                                              orderby u.FRAMED_IP
                                              select new { u.USER_NAME, u.FULL_NAME, u.FRAMED_IP, u.MAC_ADDRESS };
                            if (OnlineUsers.Any())
                            {
                                bindingSource.DataSource = OnlineUsers;
                                dataGridView1.DataSource = bindingSource;

                                dataGridView1.Columns["USER_NAME"].HeaderText = "登录名";
                                dataGridView1.Columns["FULL_NAME"].HeaderText = "姓名";
                                dataGridView1.Columns["FRAMED_IP"].HeaderText = "IP";
                                dataGridView1.Columns["MAC_ADDRESS"].HeaderText = "MAC地址";
                                toolStripStatusLabelUser.Text = "";
                                this.Refresh();
                            }
                            else
                            {
                                dataGridView1.DataSource = null;
                                MessageBox.Show(string.Format("未查询到IP地址为\"{0}\"'的在线用户!", tbContent.Text));
                            }
                        }
                    }
                    else
                    {
                        goto case -1;
                    }
 
                    break;
                }
                case 2: //MAC地址
                {
                    if (!string.IsNullOrEmpty(tbContent.Text))
                    {
                        dataGridView1.DataSource = null;
                        using (var eaddbContext = new EadEntitiesModel())
                        {
                            var OnlineUsers = from u in eaddbContext.Vw_onlines
                                              where u.MAC_ADDRESS.Replace(":","")==tbContent.Text.Replace("-", "").Replace(":","").ToUpper()
                                              select new { u.USER_NAME, u.FULL_NAME, u.FRAMED_IP, u.MAC_ADDRESS };
                            if (OnlineUsers.Any())
                            {
                                bindingSource.DataSource = OnlineUsers;
                                dataGridView1.DataSource = bindingSource;

                                dataGridView1.Columns["USER_NAME"].HeaderText = "登录名";
                                dataGridView1.Columns["FULL_NAME"].HeaderText = "姓名";
                                dataGridView1.Columns["FRAMED_IP"].HeaderText = "IP";
                                dataGridView1.Columns["MAC_ADDRESS"].HeaderText = "MAC地址";
                                toolStripStatusLabelUser.Text = "";
                                this.Refresh();
                            }
                            else
                            {
                                dataGridView1.DataSource = null;
                                MessageBox.Show(string.Format("未查询到Mac地址为\"{0}\"'的在线用户!", tbContent.Text));
                            }
                        }
                    }
                    else
                    {
                        goto case -1;
                    }
                    break;
                }
            }

            
        }

        private void tbContent_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode==Keys.Enter)
                btQuery1_Click(sender,e);
        }

    }
}
